/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgModuloPrincipal.pkgConfiguracionRegional;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.SQLException;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import pkgUtil.pkgFrameworkXML.pkgEntity.RXmlObject;
import pkgUtil.pkgFrameworkXML.pkgEntity.WXmlObject;
import pkgUtil.pkgFrameworkXML.pkgSupport.HelperReadXml;
import pkgUtil.pkgFrameworkXML.pkgSupport.HelperWriteXml;
import pkgUtil.pkgFrameworkXML.pkgSupport.XmlReaderException;
import pkgUtil.pkgFrameworkXML.pkgSupport.XmlWriterException;
import pkgUtil.pkgJavaDB.GestorDB;

/**
 *
 * @author Administrador
 */
public class GestorConfigRegional {

    private PllaConfiguracionRegional plla;

    public GestorConfigRegional() {
    }

    public void crearXmlConfiguracionRegional(ContextEntity ctxEntity) throws XmlWriterException, FileNotFoundException, IOException {
        WXmlObject xmlObject = new WXmlObject(Constants.NAME_XML_CONTEXT, Constants.NAME_XSD_VALIDATION, Constants.ENUM_XML_ELEMENTS.ROOT_ELEMENT.toString());

        HelperWriteXml helperWrite = new HelperWriteXml(xmlObject);
        Element eLocale = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.LOCALE_ELEMENT.toString(), null);
        Element eLanguage = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.LANGUAGE_ELEMENT.toString(), ctxEntity.getLanguage());
        Element eCountry = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.COUNTRY_ELEMENT.toString(), ctxEntity.getCountry());

        helperWrite.addChildToElement(eLocale, eLanguage);
        helperWrite.addChildToElement(eLocale, eCountry);

        Element eDecimalFormatSeparators = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.DECIMAL_FORMAT_SEPARATORS_ELEMENT.toString(), null);
        Element eDecimalSeparator = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.DECIMAL_SEPARATOR_ELEMENT.toString(), String.valueOf(ctxEntity.getSepDecimales()));
        Element eGroupingSeparator = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.GROUPING_SEPARATOR_ELEMENT.toString(), String.valueOf(ctxEntity.getSepMiles()));

        helperWrite.addChildToElement(eDecimalFormatSeparators, eDecimalSeparator);
        helperWrite.addChildToElement(eDecimalFormatSeparators, eGroupingSeparator);

        helperWrite.addChildToRoot(eLocale);
        helperWrite.addChildToRoot(eDecimalFormatSeparators);

        helperWrite.buildXml();
    }

    public void crearXml_ConfiguracionReg_Predeterminada() throws XmlWriterException, FileNotFoundException, IOException {
        WXmlObject xmlObject = new WXmlObject(Constants.NAME_XML_CONTEXT, Constants.NAME_XSD_VALIDATION, Constants.ENUM_XML_ELEMENTS.ROOT_ELEMENT.toString());

        HelperWriteXml helperWrite = new HelperWriteXml(xmlObject);
        Element eLocale = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.LOCALE_ELEMENT.toString(), null);
        Element eLanguage = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.LANGUAGE_ELEMENT.toString(), Constants.ENUM_LOCALE.LANGUAGE_ESPANIOL.toString());
        Element eCountry = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.COUNTRY_ELEMENT.toString(), Constants.ENUM_LOCALE.COUNTRY_ARGENTINA.toString());

        helperWrite.addChildToElement(eLocale, eLanguage);
        helperWrite.addChildToElement(eLocale, eCountry);

        Element eDecimalFormatSeparators = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.DECIMAL_FORMAT_SEPARATORS_ELEMENT.toString(), null);
        Element eDecimalSeparator = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.DECIMAL_SEPARATOR_ELEMENT.toString(), Constants.SEP_STR_DECIMAL_DEFAULT);
        Element eGroupingSeparator = helperWrite.buildElement(Constants.ENUM_XML_ELEMENTS.GROUPING_SEPARATOR_ELEMENT.toString(), Constants.SEP_STR_DECIMAL_DEFAULT);

        helperWrite.addChildToElement(eDecimalFormatSeparators, eDecimalSeparator);
        helperWrite.addChildToElement(eDecimalFormatSeparators, eGroupingSeparator);

        helperWrite.addChildToRoot(eLocale);
        helperWrite.addChildToRoot(eDecimalFormatSeparators);

        helperWrite.buildXml();
    }

    public ContextEntity leerConfiguracionRegional() throws JDOMException, FileNotFoundException, IOException, XmlReaderException, URISyntaxException, Exception {
        ContextEntity cEntity = new ContextEntity();

        RXmlObject xmlObject = new RXmlObject();
        xmlObject.setFileXml(Constants.NAME_XML_CONTEXT);
        xmlObject.setFileXsd(Constants.PATH_XSD_VALIDATION);

        HelperReadXml helperRead = new HelperReadXml(xmlObject);
        //Lectura Locale
        Element eLocale = helperRead.getElementFromRoot(Constants.ENUM_XML_ELEMENTS.LOCALE_ELEMENT.toString());
        cEntity.setLanguage(helperRead.getChildText(eLocale, Constants.ENUM_XML_ELEMENTS.LANGUAGE_ELEMENT.toString()));
        cEntity.setCountry(helperRead.getChildText(eLocale, Constants.ENUM_XML_ELEMENTS.COUNTRY_ELEMENT.toString()));
        //Lectura Separadores
        Element eSeparator = helperRead.getElementFromRoot(Constants.ENUM_XML_ELEMENTS.DECIMAL_FORMAT_SEPARATORS_ELEMENT.toString());

        String sepDecimal = helperRead.getChildText(eSeparator, Constants.ENUM_XML_ELEMENTS.DECIMAL_SEPARATOR_ELEMENT.toString());
        String sepMiles = helperRead.getChildText(eSeparator, Constants.ENUM_XML_ELEMENTS.GROUPING_SEPARATOR_ELEMENT.toString());

        cEntity.setSepDecimales(sepDecimal.charAt(0));
        cEntity.setSepMiles(sepMiles.charAt(0));

        return cEntity;

    }

    public void setConfigContextApp() throws JDOMException, FileNotFoundException, IOException, XmlReaderException, URISyntaxException, Exception {
        ContextEntity cEntity = leerConfiguracionRegional();
        Context context = Context.getSingletonContext();
        context.setConfiguration(cEntity);
    }

    public void setConfigContextApp(ContextEntity cEntity) {
        Context context = Context.getSingletonContext();
        context.setConfiguration(cEntity);
    }

    public boolean existeContextXml() {
        File xmlFile = new File(Constants.NAME_XML_CONTEXT);
        return xmlFile.exists();
    }

    /**
     * @return the plla
     */
    protected PllaConfiguracionRegional getPllaConfigRegional() {
        return plla;
    }

    /**
     * @param plla the plla to set
     */
    protected void setPllaConfigRegional(PllaConfiguracionRegional plla) {
        this.plla = plla;
    }

    public String getDescripcionParaSimbolo(String simbolo) {
        for (int i = 0; i < Constants.SEPARATOR_NUMERIC_SYMBOLS.length; i++) {
            if (simbolo.equals(Constants.SEPARATOR_NUMERIC_SYMBOLS[i].getSymbol())) {
                return Constants.SEPARATOR_NUMERIC_SYMBOLS[i].getDescription();
            }
        }
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="Configuracion Regional mediante JAVADB">
    public ContextEntity getConfiguracionPredeterminada() {
        ContextEntity ctxEntity = new ContextEntity();
        ctxEntity.setCountry(Constants.ENUM_LOCALE.COUNTRY_ARGENTINA.toString());
        ctxEntity.setLanguage(Constants.ENUM_LOCALE.LANGUAGE_ESPANIOL.toString());
        ctxEntity.setSepMiles(Constants.SEP_STR_MILES_DEFAULT.charAt(0));
        ctxEntity.setSepDecimales(Constants.SEP_STR_DECIMAL_DEFAULT.charAt(0));
        return ctxEntity;
    }

    public void setConfigContextAppByJavaDB() throws SQLException {
        ContextEntity ctxEntity = GestorDB.getConfiguracionContexto();
        if (ctxEntity == null) {
            GestorDB.insertConfiguracionContexto(getConfiguracionPredeterminada());
            ctxEntity = GestorDB.getConfiguracionContexto();
        }
        setConfigContextApp(ctxEntity);
    }

    public ContextEntity getConfigContextAppByJavaDB() throws SQLException {
        return GestorDB.getConfiguracionContexto();
    }

    public boolean actualizarConfiguracionContextoJavaDB(ContextEntity ctxEntity) throws SQLException {
        return GestorDB.updateConfiguracionContexto(ctxEntity);
    }
    // </editor-fold>
}
